// pages/repair-detail/repair-detail.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*  */
    userInfo_sj: {},
    /*  */
    modelinput:true,
    price:'',
    count:'',
    project:'',
    remark:'',
    index:'',  //编辑添加项
    type:'',
    /*  */
    items:[],
    /* 状态 */
    status:'',
    /*  */
    confirmBtn:[
      {
        name:'维修完成'
      }
    ],
    /*  */
    saveBtn:[
      {
        name:'暂存'
      }
    ],
    /*  */
    backBtn:[
      {
        name:'返回列表'
      }
    ]
  },
  /*  */
  showTitleInfo: function(e){
    let { info } = e.currentTarget.dataset
    wx.showToast({
      icon:'none',
      title:`${info.project}x${info.count}`
    })
  },
  /* 返回维修列表 */
  backToRepairList: function(e){
    let { index } = e.currentTarget.dataset
    app.globalData.setNeedAnimation.call(this, 'backBtn', this.data.backBtn, index).then(()=>{
      var pages = getCurrentPages(); //当前页面
      var beforePage = pages[pages.length - 2]; //前一页
      wx.navigateBack({
        success: function () {
          beforePage.onLoad()
        }
      })
    })
  },
  /* 打开modal */
  insertItem:function(){
    this.setData({
      modelinput:false,
      project:'',
      count:'',
      price:'',
      remark:''
    })
  },
  /* 编辑输入 */
  modifyItem: function(e){
    let { index } = e.currentTarget.dataset
    console.log(index)
    //
    let items = this.data.items
    this.setData({
      type:'modify',
      project:items[index].project,
      name:items[index].project,
      count:items[index].count,
      price:items[index].price,
      remark:items[index].remark,
      index:index,
      modelinput: false
    })
  },
  /* 删除输入 */
  deleteItem: function(e){
    let { index } = e.currentTarget.dataset
    console.log(index)
    //
    let items = this.data.items
    items.splice(index,1)
    this.setData({
      items:items
    })
  },
  /* 新增输入 */
  confirm:function(){
    if(this.data.type=='modify'){
      let items = this.data.items
      this.setData({
        [`items[${this.data.index}]`]:{
          project:this.data.project,
          name:this.data.project,
          count:this.data.count,
          price:this.data.price,
          remark:this.data.remark
        },
        type:'',
        index:'',
        modelinput:true
      })
    }else{
      if(!this.data.project){
        wx.showToast({
          icon:'none',
          title:'请输入项目名称'
        })
        return
      }
      if(!this.data.price){
        wx.showToast({
          icon:'none',
          title:'请输入金额'
        })
        return
      }
      const item2 = this.data.items
      item2.push({
        project:this.data.project,
        count:this.data.count,
        price:this.data.price,
        remark:this.data.remark
      })
      this.setData({
        modelinput:true,
        items:item2
      })
    }
  },
  /* 关闭modal */
  cancel:function(){
    this.setData({
      modelinput:true
    })
  },
  /* 获取项目输入 */
  getProject: function(e){
    this.setData({
      project:e.detail.value
    })
  },
  /* 获取数量输入 */
  getCount: function(e){
    this.setData({
      count:e.detail.value
    })
  },
  /* 获取金额输入 */
  getPrice: function(e){
    this.setData({
      price:e.detail.value
    })
  },
  /* 获取备注输入 */
  getRemark: function(e){
    this.setData({
      remark:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取用户信息
    this.setData({
      userInfo_sj: app.globalData.get_userInfo()
    })
    //
    let eventChannel = this.getOpenerEventChannel()
    eventChannel.on('open-repair-detail',data=>{
      console.log(data)
      this.setData({
        status:data.info.status
      })
      this.getDefendedInfo(data.info.id)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  
  /*  */
  test: function(){
    wx.switchTab({
      url:'/pages/index/index'
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDefendedInfo:function(defendedid){
    wx.showLoading({
      title: '获取维修详情',
      mask: true
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/wx/getDefendedInfo',
        method: 'POST',
        header: this.data.userInfo_sj.header,
        data: util.json2Form({
          id:defendedid
        }),   //
        success: res => {
          console.log(res)
          //token失效
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1' ) {
            this.setData({
              id:defendedid,
              name:res.data.body.name,
              mobile:res.data.body.mobile,
              licenseNo:res.data.body.licenseNo,
              carvin:res.data.body.carvin,
              engineNo:res.data.body.engineNo,
              appealTime:util.string2date(res.data.body.appealTime),
              address:res.data.body.address,
              caseMobile:res.data.body.caseMobile,
              explore:res.data.body.explore,
              exploreMobile:res.data.body.exploreMobile,
              status:res.data.body.status,
              /*  */
              items:res.data.body.items
            })
          } else {
            wx.showToast({
              icon: 'none',
              title: ''+res.data.msg
            })
            reject()
          }
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '获取数据失败'
          })
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  
  project:function(e){
    this.setData({
      project:e.detail.value
    })
  },
  price:function(e){
    this.setData({
      price:e.detail.value
    })
  },
  remark:function(e){
    this.setData({
      remark:e.detail.value
    })
  },
  finish:function(){
    wx.showLoading({
      title: '维修完成',
      mask: true
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/wx/updateDefendStatus',
        method: 'POST',
        header: this.data.userInfo_sj.header,
        data: util.json2Form({
          id:this.data.id,
          status:'4',
          items:JSON.stringify(this.data.items)
        }),   //
        success: res => {
          console.log(res)
          //token失效
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1' ) {
            this.repairBack(true)
          } else {
            wx.showToast({
              icon: 'none',
              title: ''+res.data.msg
            })
            reject()
          }
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '获取数据失败'
          })
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  nofinish:function(){
    wx.showLoading({
      title: '维修暂存',
      mask: true
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: app.globalData.Url + '/wx/updateDefendStatus',
        method: 'POST',
        header: this.data.userInfo_sj.header,
        data: util.json2Form({
          id:this.data.id,
          items:JSON.stringify(this.data.items)
        }),   //
        success: res => {
          console.log(res)
          //token失效
          if(app.globalData.check_token(res.data.code))return new Promise((resolve,reject)=>{})
          if (res.data.code == '1' ) {
            this.repairBack(false)
          } else {
            wx.showToast({
              icon: 'none',
              title: ''+res.data.msg
            })
            reject()
          }
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '获取数据失败'
          })
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    })
  },
  repairBack:function(isRefresh){
    var pages = getCurrentPages(); //当前页面
    var beforePage = pages[pages.length - 2]; //前一页
    wx.navigateBack({
      success: function () {
        if(isRefresh){
          beforePage.onLoad();
        } // 执行前一个页面的onLoad方法
      }
    });
  }
})